const app = getApp()

Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    StatusBar: app.globalData.StatusBar,
    theme: app.globalData.theme, //全局颜色变量
    orderInfo: null,
    id: null,
    callPay: false, //是否直接调起支付
    opacity: 0,
    addressComponent: {
      city: null,
      address: null,
      longitude: null,
      latitude: null
    },
  },
  onShow() {
    app.initPage()
      .then(res => {
        this.data.addressComponent = app.globalData.addressComponent
        this.orderGet(this.data.id)
      })
  },
  onLoad(options) {
    this.setData({
      id: options.id
    })
    if (options.callPay) {
      this.setData({
        callPay: true
      })
    }
  },
  orderGet(id) {
    let that = this
    app.api.providerOrderGet(id, {
      longitude: that.data.addressComponent.longitude,
      latitude: that.data.addressComponent.latitude
    }).then(res => {
      let orderInfo = res.data
      if (!orderInfo) {
        wx.redirectTo({
          url: '/pages/order/order-list/index'
        })
      }
      this.setData({
        orderInfo: orderInfo
      })
      setTimeout(function () {
        that.setData({
          callPay: false
        })
      }, 4000)
    })
  },
  //复制内容
  copyData(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.data
    })
  },
  orderCancel() {
    let id = this.data.orderInfo.id
    this.orderGet(id)
  },
  orderDel() {
    wx.navigateBack()
  },
  unifiedOrder() {
    this.onShow()
  },
  countDownDone() {
    this.orderGet(this.data.id)
  },
  // 跳转地图导航
  jumpMap(e) {
    let order = this.data.orderInfo
    wx.openLocation({
      latitude: Number(order.latitude),
      longitude: Number(order.longitude),
      scale: 15,
      address: order.userAddress
    })
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.orderInfo.picUrls,
      current: e.currentTarget.dataset.url
    });
  },
  ViewImage2(e) {
    wx.previewImage({
      urls: this.data.orderInfo.completePicUrls,
      current: e.currentTarget.dataset.url
    });
  },
  ViewImage3(e) {
    wx.previewImage({
      urls: this.data.orderInfo.goodsAppraises.picUrls,
      current: e.currentTarget.dataset.url
    });
  },
  BackPage() {
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 监听用户滑动页面事件 计算顶部tabs透明度
   */
  onPageScroll(e) {
    let value = e.scrollTop / 100;
    value > 1 && (value = 1);
    this.setData({
      opacity: value
    })
  },
  // 一键拨号
  bohaoClick(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  // 立即接单
  acceptOrder(e) {
    let that = this
    wx.showModal({
      content: '是否确认接单？',
      cancelText: '我再想想',
      confirmColor: '#ff0000',
      success(res) {
        if (res.confirm) {
          let id = that.data.orderInfo.id
          app.api.providerAcceptOrder({
            id: id
          }).then(res => {
            that.orderGet(id)
          })
        }
      }
    })
  },
  // 取消订单
  cancelOrder(e) {
    this.setData({
      cancelOrderModal: true
    })
  },
  hideCancelOrderModal() {
    this.setData({
      cancelOrderModal: false
    })
  },
  cancelOrderEvent(e) {
    this.hideCancelOrderModal()
    wx.reLaunch({
      url: '/pages/home/index',
    })
  },
  // 接单反馈
  completeOrder(e) {
    this.setData({
      completeOrderModal: true
    })
  },
  hideCompleteOrderModal() {
    this.setData({
      completeOrderModal: false
    })
  },
  completeOrderEvent(e) {
    this.hideCompleteOrderModal()
    this.orderGet(this.data.id)
  }
})